local qunxian3__hanniba_bingxiang = fk.CreateSkill {
  name = "qunxian3__hanniba_bingxiang",
}

Fk:loadTranslationTable {
  ["qunxian3__hanniba_bingxiang"] = "冰箱",
  [":qunxian3__hanniba_bingxiang"] = [[当你发动【烹饪】消耗【食材】标记时，根据消耗的标记获得以下增益直到你的下个额定出牌阶段开始时:
<br/>（头部）所有人手牌对你可见，你使用普通锦囊牌时额外结算一次(【借刀杀人】除外)，
<br/>（四肢）你可以使你本回合所有伤害牌造成的伤害+1，
<br/>（内脏）你失去红色牌时恢复一点体力，失去黑色牌时弃置一名目标任意区域一张牌，
<br/>（坤）你受到非自身造成的伤害时令伤害来源本回合内随机一个技能失效]],
  ["@qunxian3__hanniba_bingxiang"] = "冰箱",
  ['#qunxian3__hanniba_bingxiang-choose_players'] = '冰箱:请弃置一名角色区域内一张牌',
  ['#qunxian3__hanniba_bingxiang-choose_card'] = '冰箱：请弃置其一张牌',
  ['#qunxian3__hanniba_bingxiang-choose_skills'] = '冰箱：你可以令其一个随机技能本回合内失效',
}

qunxian3__hanniba_bingxiang:addEffect(fk.EventPhaseStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__hanniba_bingxiang") and player.phase == Player.Play and
        #player:getTableMark("@qunxian3__hanniba_bingxiang") > 0 and data.reason == "game_rule"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@qunxian3__hanniba_bingxiang", 0)
  end
})

qunxian3__hanniba_bingxiang:addEffect("visibility", {
  card_visible = function(self, player, card)
    if player:hasSkill("qunxian3__hanniba_bingxiang") and Fk:currentRoom():getCardArea(card.id) == Card.PlayerHand and
        table.contains(player:getTableMark("@qunxian3__hanniba_bingxiang"), "头") then
      return true
    end
  end
})

qunxian3__hanniba_bingxiang:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__hanniba_bingxiang") and
        table.contains(player:getTableMark("@qunxian3__hanniba_bingxiang"), "头") and data.card and
        data.card:isCommonTrick() and data.card.trueName ~= "collateral"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.additionalEffect = (data.additionalEffect or 0) + 1
  end
})

qunxian3__hanniba_bingxiang:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__hanniba_bingxiang") and
        table.contains(player:getTableMark("@qunxian3__hanniba_bingxiang"), "肢") and
        data.card and data.card.is_damage_card
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data:changeDamage(1)
  end
})

qunxian3__hanniba_bingxiang:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__hanniba_bingxiang") and
        table.contains(player:getTableMark("@qunxian3__hanniba_bingxiang"), "肢")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:removeTableMark(player, "@qunxian3__hanniba_bingxiang", "肢")
  end
})

qunxian3__hanniba_bingxiang:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill("qunxian3__hanniba_bingxiang") and
        table.contains(player:getTableMark("@qunxian3__hanniba_bingxiang"), "脏") then
      for index, move in ipairs(data) do
        if move.from == player and move.to ~= player then
          for index, info in ipairs(move.moveInfo) do
            return Fk:getCardById(info.cardId).color == Card.Red or
                Fk:getCardById(info.cardId).color == Card.Black
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for index, move in ipairs(data) do
      if move.from == player and move.to ~= player then
        for index, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).color == Card.Red then
            room:recover({
              who = player,
              num = 1,
              skillName = "qunxian3__hanniba_bingxiang",
              recoverBy = player,
            })
          elseif Fk:getCardById(info.cardId).color == Card.Black then
            local target_ps = table.filter(room.alive_players, function(p, index, array)
              return #p:getCardIds("he") > 0
            end)
            local target_p = room:askToChoosePlayers(player, {
              max_num = 1,
              min_num = 1,
              targets = target_ps,
              skill_name = "qunxian3__hanniba_bingxiang",
              prompt = "#qunxian3__hanniba_bingxiang-choose_players",
              cancelable = true,
            })
            if #target_p > 0 then
              local target_c = room:askToChooseCard(player, {
                target = target_p[1],
                prompt = "#qunxian3__hanniba_bingxiang-choose_card",
                flag = "hej",
                skill_name = "qunxian3__hanniba_bingxiang",
              })
              if target_c then
                room:throwCard(target_c, "qunxian3__hanniba_bingxiang", target_p[1], player)
              end
            end
          end
        end
      end
    end
  end
})

qunxian3__hanniba_bingxiang:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__hanniba_bingxiang") and
        table.contains(player:getTableMark("@qunxian3__hanniba_bingxiang"), "坤") and data.from and data.from ~= player
  end,
  on_cost = function(self, event, target, player, data)
    local in_skills = {}
    for index, skill in ipairs(data.from:getSkillNameList()) do
      if data.from:hasSkill(skill) then
        table.insertIfNeed(in_skills, skill)
      end
    end
    event:setCostData(self, { in_skills = in_skills })
    return #in_skills > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local in_skills = event:getCostData(self).in_skills
    if #in_skills > 0 then
      room:invalidateSkill(data.from, table.random(in_skills), "-turn")
    end
  end
})

return qunxian3__hanniba_bingxiang
